import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraVectorCollectionApi } from '#/api/infra/vector/vectorCollection';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { getKnowledgeCategoriesList } from '#/api/ai/knowledge/knowledgeCategories';
import { getLlmSimpleList } from '#/api/ai/llm/llmModel';
import { getVectorDbInstancePage } from '#/api/infra/vector/vectorDbInstance';
import DictTag from '#/components/dict-tag/dict-tag.vue';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'vectorDbId',
      label: '向量数据库实例',
      rules: 'required',
      component: 'ApiPagingSelect',
      componentProps: {
        allowClear: true,
        api: getVectorDbInstancePage,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择向量数据库实例',
      },
    },
    {
      fieldName: 'name',
      label: '集合名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入集合名称',
      },
    },
    {
      fieldName: 'categoryId',
      label: '场景分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getKnowledgeCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择场景分类',
        treeDefaultExpandAll: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'knowledgeType',
      label: '知识类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_VECTOR_KNOWLEDGE_TYPE),
        placeholder: '请选择知识类型',
      },
    },
    {
      fieldName: 'vectorDim',
      label: '向量维度',
      rules: 'required',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder:
          '请输入向量维度（如 768/1536，需与 Embedding 模型输出一致）',
      },
    },
    {
      fieldName: 'metricType',
      label: '距离度量方式',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_VECTOR_METRIC_TYPE),
        placeholder: '请选择距离度量方式',
      },
    },
    {
      fieldName: 'description',
      label: '集合描述',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入集合描述',
      },
    },
    {
      fieldName: 'status',
      label: '集合状态',
      rules: 'required',
      component: 'Select',
      defaultValue: 'normal',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_VECTOR_COLLECTION_STATUS),
        placeholder: '请选择集合状态',
      },
    },
    {
      fieldName: 'recordCount',
      label: '向量记录数',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入向量记录数（定期同步自数据库，默认 0）',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'hotLevel',
      label: '热度等级',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '热门',
            value: 'hot',
          },
          {
            label: '普通',
            value: 'warm',
          },
          {
            label: '冷',
            value: 'cold',
          },
        ],
        placeholder: '请输入热度等级',
      },
    },
    {
      fieldName: 'updateStrategy',
      label: '更新策略',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '高频更新',
            value: 'dynamic',
          },
          {
            label: '低频更新',
            value: 'stable',
          },
        ],
        placeholder: '请选择更新策略',
      },
    },
    {
      fieldName: 'embeddingModelId',
      label: '向量模型',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: () => getLlmSimpleList({ supportEmbed: true }),
        labelField: 'title',
        valueField: 'id',
        placeholder: '请选择向量模型编号',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '向量数据库实例',
      field: 'vectorDbName',
    },
    {
      label: '集合名称',
      field: 'name',
    },
    {
      label: '分类',
      field: 'categoryName',
    },
    {
      label: '知识类型',
      field: 'knowledgeType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_VECTOR_KNOWLEDGE_TYPE,
          value: val,
        }),
    },
    {
      label: '向量维度',
      field: 'vectorDim',
    },
    {
      label: '距离度量方式',
      field: 'metricType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_VECTOR_METRIC_TYPE,
          value: val,
        }),
    },
    {
      label: '集合描述',
      field: 'description',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '集合状态',
      field: 'status',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_VECTOR_COLLECTION_STATUS,
          value: val,
        }),
    },
    {
      label: '向量记录数',
      field: 'recordCount',
    },
    {
      label: '热度等级',
      field: 'hotLevel',
      render: (val) =>
        h(DictTag, {
          options: [
            {
              label: '热门',
              value: 'hot',
            },
            {
              label: '普通',
              value: 'warm',
            },
            {
              label: '冷',
              value: 'cold',
            },
          ],
          value: val,
        }),
    },
    {
      label: '更新策略',
      field: 'updateStrategy',
      render: (val) =>
        h(DictTag, {
          options: [
            {
              label: '高频更新',
              value: 'dynamic',
            },
            {
              label: '低频更新',
              value: 'stable',
            },
          ],
          value: val,
        }),
    },
    {
      label: '向量模型编号',
      field: 'embeddingModelId',
    },
    {
      label: '向量模型标识',
      field: 'embeddingModel',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'vectorDbId',
      label: '向量数据库实例',
      component: 'ApiPagingSelect',
      componentProps: {
        allowClear: true,
        api: getVectorDbInstancePage,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择向量数据库实例',
      },
    },
    {
      fieldName: 'name',
      label: '集合名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入集合名称',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getKnowledgeCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择场景分类',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'knowledgeType',
      label: '知识类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.INFRA_VECTOR_KNOWLEDGE_TYPE),
        placeholder:
          '请选择知识类型（text/structured/image，对应存入的知识形式）',
      },
    },
    {
      fieldName: 'metricType',
      label: '距离度量方式',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.INFRA_VECTOR_METRIC_TYPE),
        placeholder: '请选择距离度量方式（L2/IP/COSINE）',
      },
    },
    {
      fieldName: 'status',
      label: '集合状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.INFRA_VECTOR_COLLECTION_STATUS),
        placeholder: '请选择集合状态',
      },
    },
    {
      fieldName: 'hotLevel',
      label: '热度等级',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '热门',
            value: 'hot',
          },
          {
            label: '普通',
            value: 'warm',
          },
          {
            label: '冷',
            value: 'cold',
          },
        ],
        placeholder: '请选择热度等级',
      },
    },
    {
      fieldName: 'updateStrategy',
      label: '更新策略',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '高频更新',
            value: 'dynamic',
          },
          {
            label: '低频更新',
            value: 'stable',
          },
        ],
        placeholder: '请选择更新策略',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'embeddingModelId',
      label: '向量模型',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: () => getLlmSimpleList({ supportEmbed: true }),
        labelField: 'title',
        valueField: 'id',
        placeholder: '请选择向量模型编号',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<InfraVectorCollectionApi.VectorCollection>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '向量数据库集合id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'vectorDbName',
      title: '向量数据库实例',
      minWidth: 160,
    },
    {
      field: 'name',
      title: '集合名称',
      minWidth: 160,
    },
    {
      field: 'categoryName',
      title: '分类',
      minWidth: 160,
    },
    {
      field: 'knowledgeType',
      title: '知识类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_VECTOR_KNOWLEDGE_TYPE },
      },
    },
    {
      field: 'vectorDim',
      title: '向量维度',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_VECTOR_METRIC_TYPE },
      },
    },
    {
      field: 'metricType',
      title: '距离度量方式',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_VECTOR_METRIC_TYPE },
      },
    },
    {
      field: 'description',
      title: '集合描述',
      minWidth: 200,
    },
    {
      field: 'status',
      title: '集合状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_VECTOR_COLLECTION_STATUS },
      },
    },
    {
      field: 'recordCount',
      title: '向量记录数',
      minWidth: 120,
    },
    {
      field: 'hotLevel',
      title: '热度等级',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            {
              label: '热门',
              value: 'hot',
            },
            {
              label: '普通',
              value: 'warm',
            },
            {
              label: '冷',
              value: 'cold',
            },
          ],
        },
      },
    },
    {
      field: 'updateStrategy',
      title: '更新策略',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            {
              label: '高频更新',
              value: 'dynamic',
            },
            {
              label: '低频更新',
              value: 'stable',
            },
          ],
        },
      },
    },

    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'embeddingModelId',
      title: '向量模型编号',
      minWidth: 120,
    },
    {
      field: 'embeddingModel',
      title: '向量模型标识',
      minWidth: 120,
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
